import Vue from 'vue'
import vuex from '@/store'
import VueRouter from 'vue-router'
import Login from '@/views/Login.vue'
import Home from '@/views/Home.vue'
import Dashbord from '@/views/Dashbord'
import UserList from '@/views/UserList'
import RoleList from '@/views/RoleList'
import LoginLog from '@/views/LoginLog'
import Swagger from '@/views/Swagger'
import Druid from '@/views/Druid'
import DeptList from '@/views/DeptList'
import SystemInfo from '@/views/SystemInfo'
import OnlineUser from '@/views/OnlineUser'
import BusinessLog from '@/views/BusinessLog'
import Test from '@/views/Test'

Vue.use(VueRouter)

const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return routerPush.call(this, location).catch(error => error)
}

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  { path: '/home',
    redirect: '/Dashbord',
    component: Home,
    children: [
      {
        path: '/Dashbord', component: Dashbord
      },
      {
        path: '/UserList', component: UserList
      },
      {
        path: '/RoleList', component: RoleList
      },
      {
        path: '/LoginLog', component: LoginLog
      },
      {
        path: '/Swagger', component: Swagger
      },
      {
        path: '/Druid', component: Druid
      },
      {
        path: '/DeptList', component: DeptList
      },
      {
        path: '/SystemInfo', component: SystemInfo
      },
      {
        path: '/OnlineUser', component: OnlineUser
      },
      {
        path: '/BusinessLog', component: BusinessLog
      },
      {
        path: '/Test', component: Test
      }
    ] }
]

const router = new VueRouter({
  routes
})

// 挂载路由守卫
router.beforeEach((to, from, next) => {
  const username = window.sessionStorage.getItem('username')
  if (to.path === '/login') {
    console.log('跳登陆...')
    // 如果已经登陆  就直接到首页  没有登录信息就转到登录页
    if (!username) {
      return next()
    } else {
      return next('/home')
    }
  } else {
    if (!username) return next('/login')
    // 权限拦截
    if(vuex.getters.routers.includes(to.path)) {
      next() 
    } else {
      console.log('非法访问....')
      next() 
    }
  }
})
export default router
